@extends('layouts.modal')
@section('content')
    <form class="layui-form layui-card" action="/" method="post" autocomplete="off">
        <div class="layui-card-body">
            <div class="layui-form-item">
                <label class="layui-form-label label-required-next label-required-prev">{{ trans('role.role_name') }}
                    <font color="red">*</font></label>
                <div class="layui-input-block">
                    <input name="name" value='{{ isset($role) ? $role->name : "" }}' id="name" required
                           placeholder="{{ trans('role.please_input_role_name')}}"
                           class="layui-input">
                    <input type="hidden" id="id" value="{{ isset($role) ? $role->id : "" }}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">{{ trans('role.role_desc') }} <font color="red">*</font></label>
                <div class="layui-input-block">
                    <textarea name="desc" id="desc" placeholder="{{ trans('role.please_input_role_desc') }}"
                              class="layui-textarea">{{ isset($role) ? $role->desc  : "" }}</textarea>
                </div>
            </div>
            <div class="layui-form-item"></div>
            <div class="layui-form-item text-center">
                <button class="layui-btn layui-btn-sm" onclick="save()"
                        type="button">{{ trans('common.save_data') }}</button>
                <button class="layui-btn layui-btn-danger layui-btn-sm" onclick="cancel()"
                        type="button">{{ trans('common.cancel') }}</button>
            </div>
        </div>
    </form>

    <script type="text/javascript">
        function save() {
            var data = {};
            var postUrl = "";
            var id = $.trim($("#id").val());
            var name = $.trim($("#name").val());
            var desc = $.trim($("#desc").val());

            if (name == "") {
                layer.msg("{{ trans('role.name_required') }}");
                return false;
            }
            data.name = name;
            if (desc == "") {
                layer.msg("{{ trans('role.desc_required') }}");
                return false;
            }
            data.desc = desc;
            if (id == "") {
                postUrl = "{{ url('role/add') }}";
            } else {
                data.id = id;
                postUrl = "{{ url('role/edit') }}";
            }
            $.ajax({
                url: postUrl,
                type: 'POST',
                dataType: 'json',
                headers: {
                    'X-CSRF-TOKEN': "{{ csrf_token() }}"
                },
                ContentType: "application/json; charset=utf-8",
                data: data,
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg("{{ trans('common.operation_success') }}", {
                            time: 1000
                        }, function () {
                            parent.layer.closeAll();
                            parent.location.href = '{{ url('role') }}';
                        })
                    } else {
                        layer.msg(result.msg)
                    }
                }
            });
        }

        function cancel() {
            parent.layer.closeAll();
        }
    </script>
@endsection




